﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Configuration;

namespace MoveSharedQueries
{
    public static class ParseMappingXML
    {
        private static string Mappingxmlpath = ConfigurationManager.AppSettings["PathMappingfilepath"].ToString();//@"D:\CopyShare\PathMappings.xml";
        private static XmlDocument xmldoc = new XmlDocument();
        public static string GetDestinationAreaPath(string SourceAreaPath)
        {
            string DestinationAreapth = String.Empty;
            try
            {
                xmldoc.Load(Mappingxmlpath);
                XmlNodeList nodelist = xmldoc.GetElementsByTagName("AreaPaths");
                foreach (XmlNode node in nodelist )
                {
                    foreach (XmlNode  chldnodes in node.ChildNodes )
	                {

                        if (SourceAreaPath.Contains(chldnodes.Attributes["sourceAPath"].Value ))
                        {
                           DestinationAreapth = SourceAreaPath.Replace(chldnodes.Attributes["sourceAPath"].Value, chldnodes.Attributes["destAPath"].Value);
                           SourceAreaPath = DestinationAreapth;
                        }
	                }
                }

                if (String.IsNullOrEmpty(DestinationAreapth))
                {
                    DestinationAreapth = SourceAreaPath;
                }

                
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Logging.Log(ex.Message + "\n Stack Trace :" + ex.StackTrace);
                 
            }
            return DestinationAreapth;
        }

        public static string GetDestinationIterationPath(string SourceIterationPath)
        {
            string DestinatioinIterationpath=String.Empty;
            try
            {
                xmldoc.Load(Mappingxmlpath);
                XmlNodeList nodelist = xmldoc.GetElementsByTagName("IterationPaths");
                foreach (XmlNode node in nodelist)
                {
                    foreach (XmlNode chldnodes in node.ChildNodes)
                    {
                        if (SourceIterationPath.Contains(chldnodes.Attributes["sourceIPath"].Value))
                        {
                           DestinatioinIterationpath = SourceIterationPath.Replace(chldnodes.Attributes["sourceIPath"].Value, chldnodes.Attributes["destIPath"].Value);

                        }
                    }
                }

                if (String.IsNullOrEmpty(DestinatioinIterationpath))
                {
                    DestinatioinIterationpath = SourceIterationPath;
                }
             
            }
            catch (Exception ex )
            {
                
                Console.WriteLine(ex.Message);
                Logging.Log(ex.Message+"\n Stack Trace :"+ex.StackTrace );
                
            }

            return DestinatioinIterationpath;
        }

        public static string GetDestinationMarketsclause(string SourceMarketsclause)
        {
            string DestinationMarketsclause = String.Empty;
            try
            {
                xmldoc.Load(Mappingxmlpath);
                XmlNodeList nodelist = xmldoc.GetElementsByTagName("MarketsField");
                foreach (XmlNode node in nodelist)
                {
                    foreach (XmlNode chldnodes in node.ChildNodes)
                    {
                        string sourcemarketoper = chldnodes.Attributes["sourceMarkets"].Value;
                       sourcemarketoper= sourcemarketoper.Contains("&lt;&gt;") ? sourcemarketoper.Replace("&lt;&gt;","<>") : sourcemarketoper;

                        if (SourceMarketsclause.Contains(sourcemarketoper))
                        {
                            DestinationMarketsclause = SourceMarketsclause.Replace(sourcemarketoper, chldnodes.Attributes["destMarkets"].Value);
                            SourceMarketsclause = DestinationMarketsclause;
                        }
                    }
                }

                if (String.IsNullOrEmpty(DestinationMarketsclause))
                {
                    DestinationMarketsclause = SourceMarketsclause;
                }
              
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
                Logging.Log(ex.Message + "\n Stack Trace :" + ex.StackTrace);

            }

            return DestinationMarketsclause;
        }
    }
}
